package util;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Binder;
import android.provider.Settings;


import com.bigchun.woodhunter.R;

import java.util.List;

/**
 * Created by fei on 2017/12/9.
 */

public class PermissionUtil {
    /**
     * 提示打开设置对话框
     */
    public static void showPermissionDialog(final Activity activity) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(activity.getString(R.string.help));
        builder.setMessage(activity.getString(R.string.open_proess));
        // 拒绝, 退出应用
        builder.setNegativeButton(activity.getString(R.string.exit), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                activity.finish();
            }
        });

        builder.setPositiveButton(activity.getString(R.string.text_btn_set), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                startAppSettings(activity);
            }
        });
        builder.setCancelable(false);
        builder.show();
    }

    // 启动应用的设置
    public static void startAppSettings(Activity activity) {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse("package:" + activity.getPackageName()));
        activity.startActivity(intent);
    }

    /**
     * 适配小米
     * @param context
     * @return
     */
    @TargetApi(19)
    public static int CheckLocationPermission(Context context,String permisstion) {
        int result = 0;
        if (android.os.Build.VERSION.SDK_INT >= 19) {
            try {
                AppOpsManager appOpsManager = null;
                appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
                String pazkageName = context.getPackageName();
                if (appOpsManager != null) {
                    result = appOpsManager.checkOp(permisstion, Binder.getCallingUid(), pazkageName);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }
    public boolean isGpsEnable(Context context) {
        boolean result = true;
        try {
            LocationManager locationManager = null;
            locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            if (locationManager != null) {
                List<String> providers = locationManager.getAllProviders();
                if (providers != null) {
                    result = false;
                    for (int i = 0; i < providers.size(); i++) {
                        result = locationManager.isProviderEnabled(providers.get(i));
                        if (result) {
                            break;
                        }
                    }
                } else {
                    result = false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
